package com.base.controller;

import com.base.po.coupon_prestore.CouponCategoryPrestore;
import com.base.service.COUPON.CouponCategoryPrestoreService;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 365优惠券分类管理服务端
 *
 * @author K
 */
@Controller
@RequestMapping("/admin")
public class Index_CouponCategoryPrestore {
    @Autowired
    private CouponCategoryPrestoreService couponCategoryPrestoreService; // 分类信息

    /**
     * 商品分类首页
     *
     * @param request       参数
     * @param currentPageNo 当前页
     * @return ModelAndView
     * @throws Exception
     */
    @RequestMapping("/couponCategoryPrestoreIndex")
    public ModelAndView couponCategoryPrestoreIndex(HttpServletRequest request, Integer currentPageNo) throws Exception {
        String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
        if (userid == null) return new ModelAndView("jsp/loginCheck"); // 未登录
        Map<String, Object> map = new HashMap<>(); // 存储数据对象
        // 设置分页 获取内容
        Page page = new Page(); // 实例化分页
        page.setCurrentPageNo(currentPageNo); // 设置当前页
        page.setRecordTotal(couponCategoryPrestoreService.countAll(userid)); // 设置总条数
        List<CouponCategoryPrestore> categories = couponCategoryPrestoreService.selectAll(page, userid, 1); // 分类内容集合
        map.put("categories", categories); // 对象
        map.put("pages", page); // 分页
        return new ModelAndView("jsp/index_CouponCategoryPrestore", map);
    }

    // 跳转添加分类信息
    @RequestMapping("/addCouponCategoryPrestore")
    public ModelAndView addCouponCategoryPrestore(HttpServletRequest request) throws Exception {
        String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
        if (userid == null) return new ModelAndView("jsp/loginCheck"); // 未登录
        return new ModelAndView("jsp/add_CouponCategoryPrestore");
    }

    // 跳转修改分类信息
    @RequestMapping("/updateCouponCategoryPrestore")
    public ModelAndView updateCouponCategoryPrestore(String id) throws Exception {
        Map<String, Object> map = new HashMap<>(); // 存储数据对象
        map.put("category", couponCategoryPrestoreService.selectByPrimaryKey(id)); // 对象
        return new ModelAndView("jsp/update_CouponCategoryPrestore", map);
    }
}
